﻿using DataSyncCom;
using DataSyncCommon;
using Newtonsoft.Json;
using Quartz;
using SupDataSync.Model;
using System;
using System.Linq;
using System.Threading.Tasks;

namespace ConsoleCore
{
    class SendMsgJob : IJob
    {
        public Task Execute(IJobExecutionContext context)
        {
            SendMsg();
            return Task.FromResult(true);
        }
        /// <summary>
        /// 1.查询数据 需要发送的手机号码 
        /// 2.发送消息到负责人手机
        /// 3.发送成功标记
        /// </summary> 
        private void SendMsg()
        {
            log($"执行发送消息{DateTime.Now}");
            try
            {
                using (var db = DbHelperMySql.GetSqlSugarDb())
                {
                    var monitors = db.Queryable<sale_order_monitor>().Where(x => x.IS_CONFIRM == 0).ToList();
                    if (monitors.Count == 0) {
                        log("没有需要发送的消息");
                    }
                    var sus = monitors.Select(x => x.suppliername).Distinct().ToList();
                    foreach (var su in sus)
                    {
                        var mos = monitors.Where(x => x.suppliername == su).ToList();
                        var scbills = mos.Where(x => x.customercode == "20510002").Select(x => x.bill_no).ToList();
                        var gzbills = mos.Where(x => x.customercode != "20510002").Select(x => x.bill_no).ToList();
                        //var gz = string.Join(",", gzbills.ToArray()).Length > 20 ? string.Join(",", gzbills.ToArray()).Substring(0, 15) + "..." : string.Join(",", gzbills.ToArray());
                        //var sc = string.Join(",", scbills.ToArray()).Length > 20 ? string.Join(",", scbills.ToArray()).Substring(0, 15) + "..." : string.Join(",", scbills.ToArray());
                        var msg = "";// =gzbills.Count==0 ?"":$" 贵州:{gzbills.Count}个";
                        msg += scbills.Count == 0 ? "" : $"四川中烟共有:{scbills.Count}个";
                        var grpmo = mos.GroupBy(x => x.month);
                        var bizmonth = "";
                        foreach (var month in grpmo) {
                            var months = month.Key.Substring(5);
                            bizmonth += $"{months}月{month.Count()}个，";
                        }
                        bizmonth= bizmonth.TrimEnd('，');
                        if (!string.IsNullOrEmpty(msg))
                        {
                            var msgser = JsonConvert.SerializeObject(new SmsModel
                            { supplier = mos.First().suppliername, ten_name = msg , bizmonth = bizmonth });
                            if (string.IsNullOrEmpty(mos.First().phone)) {
                                log($"{mos.First().suppliername}手机号码是空");
                                continue;
                            }
                            var msgreturn = Common.SendSms(mos.First().phone, "SMS_200189105", msgser);//您好,尊敬的${supplier}供应商，${ten_name}销售订单，具体是${bizmonth}请注意提取查收。
                            //【四元星河云】您好，尊敬的武汉艾特纸塑包装有限公司供应商，四川中烟有: 5个销售订单，具体是08月3个，09月2个，请注意提取查收
                            if (msgreturn.Code == "OK")
                            {
                                log($"发送消息给手机{mos.First().phone} 内容【您好,尊敬的{mos.First().suppliername}供应商，{msg}销售订单，具体是{bizmonth}请注意提取查收。】");
                                foreach (var mo in mos)
                                {
                                    mo.IS_CONFIRM = 1;
                                    db.Updateable(mo).ExecuteCommand();
                                }
                            }
                            else
                            {
                                log(msgreturn.Code + " 发送消息失败  " + msgreturn.Message + $"参数{mos.First().phone} SMS_200189105 {msgser}");
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                log(ex.Message);
                throw;
            }
        }
        private void log(string str)
        {
            Console.WriteLine(str);
            LogHelper.CreateLoger().Error(str);
        }

       
    }
}
